package com.example.mwavlearndemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import com.example.mwavlearndemo.av.data.adapter.FunctionAdapter;
import com.example.mwavlearndemo.av.data.bean.Function;
import com.example.mwavlearndemo.av.ui.base.IActivityInit;
import com.example.mwavlearndemo.av.util.FunctionUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IActivityInit {
    private RecyclerView recyclerView;
    private final List<Function> functionList = new ArrayList<>();
    private FunctionAdapter functionAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        initData();
        bindEvent();
    }

    @Override
    public void findView() {
        recyclerView = findViewById(R.id.recycleview);
    }

    @Override
    public void initData() {
        initFunctions();
        checkPermission();
    }

    @Override
    public void bindEvent() {

    }
    private void initFunctions(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        functionAdapter = new FunctionAdapter(functionList);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(functionAdapter);
    }
    private void checkPermission(){
        List<String> permissionList = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
                PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.CAMERA);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) !=
                PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.RECORD_AUDIO);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
        if (permissionList.size() > 0){
            ActivityCompat.requestPermissions(this,permissionList.toArray(new String[0]),1);
        }else {
            updateFunction();
        }
    }
    private void updateFunction(){
        functionList.clear();
        functionList.addAll(FunctionUtils.FUNCTION_LIST);
        functionAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                for (int grantResult :
                        grantResults) {
                    if (grantResult != PackageManager.PERMISSION_GRANTED){
                        Toast.makeText(this, "need permissions", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                    }
                }
                updateFunction();
                break;
        }
    }
}